package com.clayecloud.module_visitor.viewModels;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_common.apis.VisitorApi;
import com.clayecloud.lib_common.apis.entity.visitor.ClientVisitor;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.lib_common.utils.MessageUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class VisitorModelViewModel extends ViewModel implements ToastAction {

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private VisitorApi visitorApi;

    private MutableLiveData<Boolean> insertState;

    public MutableLiveData<Boolean> getInsertState() {
        return insertState;
    }

    public void setInsertState(MutableLiveData<Boolean> insertState) {
        this.insertState = insertState;
    }

    public void leaddata(){
        visitorApi = HttpUtil.getInstance().create(VisitorApi.class);
        MutableLiveData<Boolean> insertState = new MutableLiveData<>();
        insertState.setValue(false);
        setInsertState(insertState);
    }

    public void submit(Integer type, String accesstime, String endtime, String name, String phone, String describe) throws ParseException {
        visitorApi.insert(new ClientVisitor(LoginUtil.getUserID(), name, phone, describe, accesstime, endtime, type))
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if(messageUtil.getResult()){
                            MutableLiveData<Boolean> insertState = getInsertState();
                            insertState.setValue(true);
                            setInsertState(insertState);
                        }else{
                            toast(messageUtil.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        toast("网络错误，请检查网络");
                        Log.e("VisitorModelViewModel submit error", e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });



    }


}
